package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dish_pojo.DishUnit;
import org.example.dish_pojo.Result;
import org.example.service.IDishUnitService;
import org.example.vo.QueryUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜品计量单位表 前端控制器
 * </p>
 *
 * @author mp
 * @since 2025-08-01
 */
@RestController
@RequestMapping("/dish-unit")
public class DishUnitController {

    @Autowired
    private IDishUnitService iDishUnitService;


    // 添加菜品计量单位
    @RequestMapping("/addDishUnit")
    public Result addDishUnit(@RequestBody DishUnit dishUnit) {
        boolean save = iDishUnitService.save(dishUnit);
        if (!save) {
            return new Result(500, "添加失败", null);
        }
        return new Result(200, "添加成功", null);
    }


    // 根据id回显数据
    @RequestMapping("/getUnitById")
    public Result getUnitById(@RequestParam("unitId") Integer unitId) {
        DishUnit unit = iDishUnitService.getById(unitId);
        return new Result(200, "操作成功", unit);
    }


    // 修改菜品计量单位
    @RequestMapping("/updateDishUnit")
    public Result updateDishUnit(@RequestBody DishUnit dishUnit) {
        boolean update = iDishUnitService.updateById(dishUnit);
        if (!update) {
            return new Result(500, "修改失败", null);
        }
        return new Result(200, "修改成功", null);
    }


    // 删除菜品计量单位
    @RequestMapping("/deleteDishUnit")
    public Result deleteDishUnit(Integer unitId) {
        boolean delete = iDishUnitService.removeById(unitId);
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }


    // 批量删除菜品计量单位
    @RequestMapping(value = "/deleteDishUnits", method = RequestMethod.POST)
    public Result deleteDishUnits(@RequestParam("unitIds") Integer[] unitIds) {
        boolean delete = iDishUnitService.removeByIds(Arrays.asList(unitIds));
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }


    // 分页查询菜品计量单位
    @RequestMapping("/listUnit")
    public Result listUnit(@RequestBody QueryUnit queryUnit) {
        Page<DishUnit> page = new Page<>(queryUnit.getCurrent(), queryUnit.getSize());
        QueryWrapper<DishUnit> wrapper = new QueryWrapper<>();
        wrapper.like(queryUnit.getUnitName() != null, "unit_name", queryUnit.getUnitName());
        Page<DishUnit> pageData = iDishUnitService.page(page, wrapper);
        return new Result(200, "查询成功", pageData);
    }


    // 批量导入菜品计量单位
    @RequestMapping(value = "/importDishUnits", method = RequestMethod.POST)
    public String importDishUnits(@RequestBody DishUnit[] dishUnits) {
        boolean save = iDishUnitService.saveOrUpdateBatch(Arrays.asList(dishUnits));
        if (!save) {
            return "批量导入失败";
        }
        return "批量导入成功";
    }


    // 批量导出菜品计量单位
    @RequestMapping("/exportDishUnits")
    public void exportDishUnits(HttpServletResponse response) throws IOException {
        // 查询数据
        QueryWrapper<DishUnit> wrapper = new QueryWrapper<>();
        wrapper.select("unit_id", "unit_name");
        List<DishUnit> dishUnits = iDishUnitService.list(wrapper);

        // 设置响应头
        String fileName = "dish_units.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        // 生成Excel文件并写入响应输出流
        generateExcelFile(dishUnits, response.getOutputStream());
    }


    // 生成Excel文件的方法
    private void generateExcelFile(List<DishUnit> dishUnits, OutputStream outputStream) throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建工作表
        Sheet sheet = workbook.createSheet("菜品计量单位");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("单位ID");
        headerRow.createCell(1).setCellValue("单位名称");

        // 填充数据
        for (int i = 0; i < dishUnits.size(); i++) {
            DishUnit dishUnit = dishUnits.get(i);
            Row row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(dishUnit.getUnitId());
            row.createCell(1).setCellValue(dishUnit.getUnitName());
        }

        // 自动调整列宽
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);

        // 写入输出流
        workbook.write(outputStream);

        // 关闭工作簿
        workbook.close();
    }


}

